package com.wgts.wgts.control.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.localbean.JoinPeopleData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/*
 *
 *
 *   data:2018/10/1619:22
 *
 *   参加总人数dialog
 *
 *
 */public class JoinPeoplesDialog extends AbstractCustomDialogFragment {
    private static final String JOIN_PEOPLE_DIALOG_BUNDLE = "joinPeopleDialogBundle";
    private static final String JOIN_TITLE_TEXT = "joinTitleText";

    Unbinder unbinder;
    @BindView(R.id.join_peoples_recycle)
    RecyclerView joinPeoplesRecycle;
    @BindView(R.id.close_dialog)
    ImageView closeDialog;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.placeholder_ly)
    LinearLayout placeholderLy;

    private String showText, type;

    private List<JoinPeopleData> joinPeopleDataList;
    private String titleText;
    private JoinPeopleAdapter adapter;

    @Override
    public void onStart() {
        super.onStart();
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.CENTER;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }

    @Override
    public View onCreateDialogView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            joinPeopleDataList = bundle.getParcelableArrayList(JOIN_PEOPLE_DIALOG_BUNDLE);
            titleText = bundle.getString(JOIN_TITLE_TEXT);
        }
        View view = LayoutInflater.from(getContext()).inflate(R.layout.join_peoples_dialog, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    public static JoinPeoplesDialog getJoinPeoplesDialog(String titleText, ArrayList<JoinPeopleData> dataList) {
        JoinPeoplesDialog fragment = new JoinPeoplesDialog();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(JOIN_PEOPLE_DIALOG_BUNDLE, dataList);
        bundle.putString(JOIN_TITLE_TEXT, titleText);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initView() {
        joinPeoplesRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        if (titleText != null && !titleText.equals("")){
            title.setText(titleText);
        }
        if (joinPeopleDataList != null && joinPeopleDataList.size() > 0) {
            adapter = new JoinPeopleAdapter(getContext(), joinPeopleDataList);
            joinPeoplesRecycle.setAdapter(adapter);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.close_dialog)
    public void onViewClicked() {
        dismiss();
    }


}
